<?php
use x\G;

//递归, 将树型结构变为一维数组, 并添加id
function admin_menu_arr($tree, $pid = 0) {
	$temp = [];
	foreach ($tree as $k => $v) {
		$arr = [
			"id" => $pid . "_" . $k,
			"pid" => $pid,
			"title" => $v['title'],
			"font" => "larry-icon",
			"icon" => isset($v['icon']) ? $v['icon'] : "",
			"right_url" => $v['url'] ? '/' . G::$m . strtolower($v['url']) : $pid . "_" . $k,
		];
		$arr['url'] = url($arr['right_url']);
		$temp[] = $arr;
		if (isset($v['children'])) {
			$return = admin_menu_arr($v['children'], $pid . "_" . $k);
			$temp = array_merge($temp, $return);
		}
	}
	return $temp;
}

//递归, 将一维数组还原为前端需要的结构
function admin_menu_tree($temp, $pid = 0) {
	$tree = [];
	foreach ($temp as $k => $v) {
		if ($v['pid'] === $pid) {
			unset($temp[$k]);
			$v['children'] = admin_menu_tree($temp, $v['id']);
			if (!$v['children']) {
				unset($v['children']);
			}
			$tree[] = $v;
		}
	}
	return $tree;
}
//递归, 输出为html
function admin_menu_html($tree) {
	$html = '';
	foreach ($tree as $v) {
		if (!isset($v['children'])) {
			$arr = explode("/", $v['right_url']);
			if (!isset($arr[2])) {
				continue;
			}
			$c = $arr[1] . "/" . $arr[2];
			$html .= "<li>
          <label><input _url='{$v['right_url']}' type='checkbox' lay-ignore name='content[]' value='{$v['right_url']}' /> {$v['title']}</label>
          <label><input _url='/{$c}/add' type='checkbox' lay-ignore name='content[]' value='/{$c}/add' /> 添加</label>
          <label><input _url='/{$c}/edit' type='checkbox' lay-ignore name='content[]' value='/{$c}/edit' /> 编辑</label>
          <label><input _url='/{$c}/del' type='checkbox' lay-ignore name='content[]' value='/{$c}/del' /> 删除</label>
          <label><input _url='/{$c}/del' type='checkbox' lay-ignore name='content[]' value='/{$c}/delall' /> 批量删除</label>
          <label><input _url='/{$c}/detail' type='checkbox' lay-ignore name='content[]' value='/{$c}/detail' /> 查看</label>
          </li>";
		} else {
			$html .= "<li><label><input class='all' _url='{$v['right_url']}' type='checkbox' lay-ignore name='content[]' value='{$v['right_url']}' /> {$v['title']}</label>";
			$html .= admin_menu_html($v['children']);
			$html = $html . "</li>";
		}
	}
	return $html ? '<ul>' . $html . '</ul>' : $html;
}
/**
 * 签名
 * @param unknown $data 数组
 * @param unknown $keys app key值
 * @return true or false
 */
/**
 * 签名
 * @param unknown $data 数组
 * @param unknown $keys app key值
 * @return true or false
 */
function sign($data) {
	#配置: 'partner'=>'秘钥',
	$app_config = [
		"5100" => 'ios_app_secret', #ios
		"5200" => 'android_app_secret', #安卓
		"5300" => 'h5_app_secret', #h5
		"5400" => 'pc_app_secret', #pc
		"5500" => 'xcx_app_secret', #小程序
		"9000" => "a3e028b28efc56d06e4f48f9ae81d236", #线下收银系统
	];
	if (isset($data['sign'])) {
		unset($data['sign']);
	}

	//partner, key参与签名
	if (!isset($data['partner']) || !in_array($data['partner'], array_keys($app_config))) {
		return -1;
	}
	$data['key'] = $app_config[$data['partner']];
	ksort($data);
	$str = '';
	foreach ($data as $k => $v) {
		$str .= "&$k=$v";
	}
	$str = ltrim($str, "&");
	return strtoupper(md5($str));
}

//加密方式
function new_com_password($password, $salt = '') {
	return md5(md5($password) . $salt);
}

//10 =》 27进制
function en_id($num = 0) {
	$arr = array('1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'h', 'i', 'j', 'k', 'm', 'n', 'p', 'r', 't', 'u', 'v', 'w', 'x', 'y', 'z');
	$t = '';
	while ($num > 0) {
		$t = $arr[$num % count($arr)] . $t;
		$num = floor($num / count($arr));
	}
	return $t == '' ? 0 : $t;
}
//27 =》 10进制
function de_id($d = 'a') {
	$num = 0;
	$i = 1;
	$arr = array('1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'h', 'i', 'j', 'k', 'm', 'n', 'p', 'r', 't', 'u', 'v', 'w', 'x', 'y', 'z');
	$d = str_split($d);
	foreach ($d as $key => $n) {
		$num += pow(count($arr), count($d) - $i) * array_search($n, $arr);
		$i++;
	}
	return $num;
}

//获取用户客户端token
function tokenType() {
	if (in_array(G::$exts['partner'], [5100, 5200])) {
		$field = 'app_token';
	} else {
		$field = 'wap_token';
	}
	return $field;
}
